<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
  <script src="../js/vuex.js"></script>
</head>
<body>
  <div id="app">
    {{msg}}
    <br>
    {{fmtMsg}}
    <br>
    {{tableData}}
    <br>
    <!-- <button @click="$store.commit('changeMsg','hello mutation')">提交突变改变数据</button> -->
    <button @click="changeMsg('hello')">提交突变改变数据</button>
  </div>
  <script>
    // 从vuex中结构辅助函数
    let { mapState, mapGetters, mapMutations, mapActions } = Vuex
    let store = new Vuex.Store({
      state: {
        vuexMsg: 'hello vuex',
        tableData: []
      },
      getters: {
        fmtMsg(state) {
          return state.vuexMsg.toUpperCase()
        }
      },
      mutations: {
        changeMsg(state,payload){
          state.vuexMsg = payload
        },
        changeTableData(state,payload){
          state.tableData = payload
        }
      },
      actions: {
        findAll(context) {
          let arr = [
            { name:'张三', age: 12 },
            { name:'李四', age: 15 },
          ]
          // 提交突变来修改state中的数据
          context.commit('changeTableData',arr)
          // console.log(context);
        }
      }
    })
    let vm = new Vue({
      el: '#app',
      store,
      data: {

      },
      computed: {

        // ...Vuex.mapState(['vuexMsg','tableData']),
        ...mapState({
          msg: 'vuexMsg',
          tableData: 'tableData'
        }),
        ...mapGetters(['fmtMsg']),
        // msg() {
        //   // console.log(this.$store);
        //   return this.$store.state.vuexMsg
        // },
        // fmtMsg() {
        //   return this.$store.getters.fmtMsg
        // },
        // tableData() {
        //   return this.$store.state.tableData
        // }
      },
      methods: {
        ...mapMutations(['changeMsg']),
        ...mapActions(['findAll'])
      },
      created() {
        // this.$store.dispatch('findAll')
        this.findAll()
      }
    })
  </script>
</body>
</html>